<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/10/9
 * Time: 8:27
 */
namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Session;
class System extends Common
{
    /*
     * 系统设置
     */
    public function system()
    {
        //获取数据库的信息用于展示
        if (Request::instance()->isAjax()) {
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }
            $where['is_display'] = array('eq', 0);

            $count = Db::name('system')->where($where)->count();
            $data = Db::name('system')
                ->limit($startrow, $pagesize)
                ->where($where)
                ->select();
            return json(array('code' => 0, 'msg' => '请求成功', 'count' => $count, 'data' => $data));

        } else {
            return $this->fetch();
        }
    }

    /*
     * 修改系统设置
     */
    /*
    * 企业信息的修改页面
    */
    public function editCompany()
    {
        //获取id
        $id = $_GET['id'];
        Session::set('company_id', $id);
        //查询数据库
        $data = Db::name('system')
            ->where('id', $id)
            ->find();
        if ($data !== false) {
            $this->assign('data', $data);
        }
        return $this->fetch();
    }

    /*
     * 企业信息的修改
     */
    public function editXiuCompany()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $post = $post['data'];

            if (!$post['company_logo']) {
                return ['code' => 1, 'msg' => '请上传logo'];
            }
            $saveData = array(
                'company_email' => $post['company_email'],
                'company_phone' => $post['company_phone'],
                'company_logo' => $post['company_logo'],
                'company_code' => $post['company_code'],
                'create_time' => date("Y-m-d H:i:s"),

            );

            $res = Db::name('system')->where('id', Session::get('company_id'))->update($saveData);
            if ($res !== false) {
                return ['code' => 0];
            } else {
                return ['code' => 2];
            }
        }
    }

    /*
     * 意见反馈
     */
    public function idea()
    {
        //获取数据库的信息用于展示
        if (Request::instance()->isAjax()) {
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }
            $where = [];

            $count = Db::name('idea')
                ->where($where)->count();
            $data = Db::name('idea')
                ->limit($startrow, $pagesize)
                ->where($where)
                ->order('id', 'desc')
                ->select();
            return json(array('code' => 0, 'msg' => '请求成功', 'count' => $count, 'data' => $data));

        } else {
            return $this->fetch();
        }
    }

    /*
    * 协议说明
    */
    public function agreement()
    {
        //获取数据库的信息用于展示
        if (Request::instance()->isAjax()) {
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }


            $count = Db::name('agreement')->count();
            $data = Db::name('agreement')
                ->order('id', 'desc')
                ->limit($startrow, $pagesize)
                ->select();
            return json(array('code' => 0, 'msg' => '请求成功', 'count' => $count, 'data' => $data));

        } else {
            return $this->fetch();
        }
    }

    /*
     * 添加协议
     */
    public function addAgreement()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $post = $post['data'];

            //存入到数组中
            $date = [
                'title' => $post['title'],
                'content' => $post['content'],
            ];

            $res = Db::name('agreement')->insert($date);

            if ($res) {
                return ['code' => 0];
            } else {
                return ['code' => '添加失败'];
            }
        }
    }

    /*
     * 删除协议
     */
    public function delAgreement()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();


            $res = Db::name('agreement')->where('id', $post['id'])->delete();

            if ($res !== false) {
                return "1";
            } else {
                return "2";
            }
        }
    }

    /*
  * 查看协议
  */
    public function getContent()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $data = Db::name('agreement')->field("content")->where('id', $post['id'])->find();
            if ($data) {
                return ['code' => 0, 'data' => $data['content']];
            } else {
                return ['code' => 1];
            }
        }
    }
    /*
    * 修改的战士页面
    */
    public function editAgreement()
    {
        $id = $_GET['id'];
        Session::set('id', $id);
        //查询数据库
        $data = Db::name('agreement')
            ->where('id', $id)
            ->find();
        if ($data !== false) {
            $this->assign('data', $data);
        }
        return $this->fetch();
    }
    /*
     * 修改协议
     */
    public function updateAgreement()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $post = $post['data'];
            $data = [
                'title'=>$post['title'],
                'content'=>$post['content']
            ];
            $res = Db::name('agreement')->where('id',Session::get('id'))->update($data);

            if ($res !== false) {
                return ['code' => 1];
            } else {
                return ['code' => 0];
            }
        }
    }

    public function problem()
    {
        //获取数据库的信息用于展示
        if (Request::instance()->isAjax()) {
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }


            $count = Db::name('problem')->count();
            $data = Db::name('problem')
                ->order('id', 'desc')
                ->limit($startrow, $pagesize)
                ->select();
            return json(array('code' => 0, 'msg' => '请求成功', 'count' => $count, 'data' => $data));

        } else {
            return $this->fetch();
        }
    }

    /*
     * 添加协议
     */
    public function addProblem()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $post = $post['data'];

            //存入到数组中
            $date = [
                'title' => $post['title'],
                'content' => $post['content'],
            ];

            $res = Db::name('problem')->insert($date);

            if ($res) {
                return ['code' => 0];
            } else {
                return ['code' => '添加失败'];
            }
        }
    }

    /*
     * 删除协议
     */
    public function delProblem()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();


            $res = Db::name('problem')->where('id', $post['id'])->delete();

            if ($res !== false) {
                return "1";
            } else {
                return "2";
            }
        }
    }
    /*
    * 查看问题
    */
    public function problemShow()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $data = Db::name('problem')->field("content")->where('id', $post['id'])->find();
            if ($data) {
                return ['code' => 0, 'data' => $data['content']];
            } else {
                return ['code' => 1];
            }
        }
    }

    /*
    * 修改问题页面
    */
    public function editProblem()
    {
        $id = input('id','','trim');
        Session::set('id', $id);
        //查询数据库
        $data = Db::name('problem')
            ->where('id', $id)
            ->find();
        if ($data !== false) {
            $this->assign('data', $data);
        }
        return $this->fetch();
    }
    /*
     * ajax修改问题
     */
    public function updateProblem()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $post = $post['data'];
            $data = [
                'title'=>$post['title'],
                'content'=>$post['content']
            ];
            $res = Db::name('problem')->where('id',Session::get('id'))->update($data);

            if ($res !== false) {
                return ['code' => 1];
            } else {
                return ['code' => 0];
            }
        }
    }
}